var M17FrontendEdit = new Class({
    Implements: [Options, Events],
    options: {
    },
    initialize: function(options){
        this.setOptions(options);
        this.elements = $$('.m17Fe');
        this.map = new Hash();
        //this.buildTopBar();
        //this.sorting();
        //this.editItem();
        this.addElementTools();
    },
    addElementTools: function() {
        var els = $$('.m17Ce');
        els.each(function(el){
            el.setStyle('position', 'relative');
            var toolbar = new Element('div', {
                'class': 'm17CeToolbar',
                styles: {
                    'height': el.getStyle('height')
                }
            }),
            toolbarToggler = new Element('div', {
                'class': 'm17CeToolbar-toggler',
                events: {
                    'mouseenter': function(event) {
                        el.getElement('div.m17CeToolbar-itemwrap').tween('opacity', 1);
                    }.bind(this),
                    'mouseleave': function(event) {
                        el.getElement('div.m17CeToolbar-itemwrap').tween('opacity', 0);
                    }.bind(this),
                }
            }).inject(toolbar),
            toolbarItemWrap = new Element('div', {
                'class': 'm17CeToolbar-itemwrap',
                styles: {
                    'opacity': 0
                }
            }).inject(toolbarToggler);
            toolbar.inject(el);
            this.actions.editItem(el).inject(toolbarItemWrap);
            this.actions.deleteItem(el).inject(toolbarItemWrap);
            this.actions.addItem(el, 'tl_content').inject(toolbarItemWrap);
        }, this);
    },
    actions: {
        editItem: function(el)
        {
            var _edit = new Element('a',
            {
                'href': 'contao/main.php?do=article&table=tl_content&act=edit&id='+el.get('m17Id')+'&m17FeEdit=true',
                'rel': 'lightbox[external 750 600]',
                'class': 'control m17EditPage'
            });
            return _edit;
        },
        deleteItem: function(el)
        {
            var _href = 'contao/main.php?do=article&table=tl_content&act=delete&id='+el.get('m17Id');
            var _delete = new Element('a',
            {
                'href': _href,
                'class': 'control cfe-delete',
                events: {
                    'click': function(event) {
                        event.preventDefault();
                        var securityQuestion = confirm('Wirklich löschen?');
                        if(securityQuestion) {
                            var myRequest = new Request({
                                url: _href,
                                onProgress: function(event, xhr){

                                },
                                onSuccess: function(responseText, responseXML) {
                                    el.tween('height', 0);
                                }
                            });
                            myRequest.send();
                        }
                    }.bind(this)
                }
            });
            return _delete;
        },
        addItem: function(el, type) {
            var _mode;
            if(type === 'tl_content') {
                _mode = 1;
            } else if(type === 'tl_article') {
                _mode = 2;
            }
            
            var _href = 'contao/main.php?do=article&table=tl_content&act=create&mode='+_mode+'&pid='+el.get('m17Id')+'&id='+el.get('m17pid')+'&m17FeEdit=true';
            
            var _item = new Element('a',
            {
                'href': _href,
                'rel': 'lightbox[external 750 600]',
                'class': 'control cfe-add'
            });
            
            return _item;
        }
    },
    deleteItem: function()
    {
        //Funktion zum löschen eines Elements
    },
    createItem: function()
    {
        //Funktion zum erstellen eines neuen Elements
    },
    sorting: function() {
        var _mySortables = new Sortables($$('.mod_article'),
        {
            revert: { duration: 500, transition: 'elastic:out' },
            onStart: function(element, clone)
            {
                //element.addClass('m17EditOnMove');
                $$('.m17Article').addClass('setHere');
                $$('.m17Ce').addClass('setHere');
            },
            onComplete: function(element) {
                var _parentIdMap, _mode, _query;
                var _curId = element.get('m17id');
                //Wenn es am ende oder mitten im Content steht
                if(element.getPrevious('.m17Fe'))
                {
                    _mode = 1;
                    _query = 'do=article&table=tl_content&id='+element.getPrevious('.m17Fe').get('m17pid')+'&?do=article&table=tl_content&id='+_curId+'&act=cut&mode='+_mode+'&pid='+element.getPrevious('.m17Fe').get('m17id');
                }
                //Wenn es das erste Element ist
                else if(element.getNext('.m17Fe'))
                {
                    _mode = 2;
                    _query = 'do=article&table=tl_content&id='+element.getNext('.m17Fe').get('m17pid')+'&?do=article&table=tl_content&id='+_curId+'&act=cut&mode='+_mode+'&pid='+element.getNext('.m17Fe').get('m17pid');
                }
                //Wenn der Artikel leer ist
                else
                {
                
                }
                var myRequest = new Request({method: 'get', url: 'contao/main.php' });
                myRequest.send(_query);
                $$('.m17Article').removeClass('setHere');
                $$('.m17Ce').removeClass('setHere');
            }.bind(this)
        });
        return _mySortables;
    },
    buildTopBar: function()
    {
        var _div = new Element('div',{'id': 'm17EditPageContainer'});
        _div.inject(document.body);
        var _editPageButton = this.editPageButton();
        _editPageButton.inject(_div, 'inside');
    },
    getIdAndPid: function(el)
    {
        var _getClass = el.get('class').match(/m17Edit{(.*?)}/);
        var _getPair = _getClass[1].split(',');
        var _id = _getPair[0].split('=');
        var _pid = _getPair[1].split('=');
        var _items = new Hash();
        _items.set(_id[0], _id[1]);
        _items.set(_pid[0], _pid[1]);
        return _items;
    },
    //generate the Button for editing Page
    editPageButton: function()
    {
        var _link = new Element('a',
        {
            'text': 'Seite bearbeiten',
            'href': 'contao/main.php?do=page&act=edit&id='+currentPage.pageId+'&m17FeEdit=true',
            'rel': 'lightbox[external 750 600]',
            'id': 'm17EditPage'
        });
        return _link;
    }
});

new M17FrontendEdit();